<template>
	<div class="aid">
		<div class="base">
			<p class="fixed-top">
				{{ JSON.stringify(text) == '{}' ? '添加活动分类奖项' : '修改活动分类奖项' }}
				<a-icon @click="closeFunction" type="close" />
			</p>
			<div class="main" id="new_message">
				<a-form-model ref="ruleForm" :model="form" :rules="rules">
					<a-form-model-item label="名称" prop="Title"><a-input v-model="form.Title" /></a-form-model-item>
					<a-form-model-item label="奖项类型" prop="Type">
						<a-select v-model="form.Type" style="width:100%">
							<a-select-option v-for="(item, index) in ActivityTypePrizeTypeList" :key="item.Key" :value="item.Key">{{ item.Value }}</a-select-option>
						</a-select>
					</a-form-model-item>
					<a-form-model-item label="级别" prop="LevelID">
						<a-select v-model="form.LevelID" style="width:100%">
							<a-select-option v-for="(item, index) in LevelList" :key="index" :value="item.ID">{{ item.Name }}</a-select-option>
						</a-select>
					</a-form-model-item>
					<a-form-model-item label="分数" prop="Score"><a-input type="number" v-model="form.Score" /></a-form-model-item>
					<a-form-model-item label="获奖人数" prop="PeopleNum">
						<a-input style="width:100px;" type="number" v-model="form.PeopleNum" />
						<span style="color:#f00;margin-left:10px;">填写-1代表不限制</span>
					</a-form-model-item>
					<a-form-model-item label="是否允许申请" prop="IsAgreeApply">
						<a-radio-group name="radioGroup" v-model="form.IsAgreeApply">
							<a-radio :value="true">是</a-radio>
							<a-radio :value="false">否</a-radio>
						</a-radio-group>
					</a-form-model-item>
					<a-form-model-item label="是否允许发起" prop="IsAgreeCreate">
						<a-radio-group name="radioGroup" v-model="form.IsAgreeCreate">
							<a-radio :value="true">是</a-radio>
							<a-radio :value="false">否</a-radio>
						</a-radio-group>
					</a-form-model-item>
					<a-form-model-item label="是否允许发放" prop="IsAgreeSend">
						<a-radio-group name="radioGroup" v-model="form.IsAgreeSend">
							<a-radio :value="true">是</a-radio>
							<a-radio :value="false">否</a-radio>
						</a-radio-group>
					</a-form-model-item>
					<a-form-model-item label="是否自定义分数" prop="ApplyCustomScore">
						<a-radio-group name="radioGroup" v-model="form.ApplyCustomScore">
							<a-radio :value="true">是</a-radio>
							<a-radio :value="false">否</a-radio>
						</a-radio-group>
					</a-form-model-item>
					<a-form-model-item label="" class="fixed-bottom">
						<a-button type="primary" @click="handleSubmit">{{ JSON.stringify(text) == '{}' ? '确认添加' : '确认修改' }}</a-button>
						<a-button style="margin-left: 10px;" @click="closeFunction">取消</a-button>
					</a-form-model-item>
				</a-form-model>
			</div>
		</div>
	</div>
</template>

<script>
import { AddActivityTypePrize,UpdateActivityTypePrize } from '@/api/follow';
export default {
	props: {
		text: Object,
		ActivityTypePrizeTypeList:Array,
		LevelList:Array
	},
	watch: {
		text: function(text) {
			this.currentID = text.ID
			if (!this.currentID) {
				this.form.PeopleNum='-1'
			} else {
				this.form = text;
			}
		}
	},
	data() {
		return {
			formLayout: 'horizontal',
			form: this.$form.createForm(this, {
				name: 'coordinated'
			}),
			currentID:'',
			DepartmenDropdowntList: [],
			form: {
				Title: '',
				Type:'',
				LevelID:'',
				Score:'',
				PeopleNum:'',
				ApplyCustomScore:""
			},
			rules: {
				Title: [
					{
						required: true,
						message: '请输入名称',
						trigger: 'change'
					}
				],
				Type:[
					{
						required: true,
						message: '请选择类型',
						trigger: 'change'
					}
				],
				LevelID:[
					{
						required: true,
						message: '请选择级别',
						trigger: 'change'
					}
				],
				Score: [
					{
						required: true,
						message: '请输入分值',
						trigger: 'change'
					}
				],
				PeopleNum: [
					{
						required: true,
						message: '请输入分值',
						trigger: 'change'
					}
				],
				IsAgreeApply: [
					{
						required: true,
						message: '请选择是否申请',
						trigger: 'change'
					}
				],
				IsAgreeCreate: [
					{
						required: true,
						message: '请选择是否发起',
						trigger: 'change'
					}
				],
				IsAgreeSend: [
					{
						required: true,
						message: '请选择是否发放',
						trigger: 'change'
					}
				],
				ApplyCustomScore: [
					{
						required: true,
						message: '请选择是否自定义分数',
						trigger: 'change'
					}
				],
			}
		};
	},
	methods: {
		closeFunction(data) {
			this.$emit('closeFun', data);
			this.form = {};
		},
		handleSubmit(e) {
			this.$refs.ruleForm.validate(async valid => {
				if (valid) {
					let data = this.form;
					data.ActivityTypeID = this.$route.query.activityTypeID
					let res = {}
					if (JSON.stringify(this.text) == '{}') {
						res = await AddActivityTypePrize(data);
					} else {
						res = await UpdateActivityTypePrize(data);
					}
					if (res.data.code === 0) {
						this.$message.success(res.data.msg);
						this.closeFunction('1');
					}else{
						this.$message.error(res.data.msg);
					}
				} else {
					return false;
				}
			});
		}
	}
};
</script>

<style lang="less" scoped>
	/deep/.ant-form-item {
		margin: 20px 50px;
		display: flex;
	}
	/deep/.ant-form-item-label {
		width: 100px;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		text-align: right;
	}
	/deep/.ant-form-item-control-wrapper {
		flex: 1;
	}


.ant-form-item {
	margin-bottom: 10px;
}

/deep/.ant-form {
	margin-top: 50px;
	padding-bottom: 50px;
}
</style>
